/**
 * The MIT License (MIT)
 * Copyright (c) 2020 铭软科技(mingsoft.net)
 * Permission is hereby granted, free of charge, to any person obtaining a copy of
 * this software and associated documentation files (the "Software"), to deal in
 * the Software without restriction, including without limitation the rights to
 * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
 * the Software, and to permit persons to whom the Software is furnished to do so,
 * subject to the following conditions:

 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.

 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
 * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
 * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
 * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */
package net.mingsoft.cms.action;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import io.swagger.annotations.Api;
import net.mingsoft.base.entity.ResultData;
import net.mingsoft.basic.annotation.LogAnn;
import net.mingsoft.basic.bean.EUListBean;
import net.mingsoft.basic.constant.e.BusinessTypeEnum;
import net.mingsoft.basic.util.BasicUtil;
import net.mingsoft.cms.biz.IFriendLinkBiz;
import net.mingsoft.cms.entity.FriendLinkEntity;
import springfox.documentation.annotations.ApiIgnore;

/**
 * 友情链接
 */
@Api(value = "友情链接接口")
@Controller("cmsFriendLinkAction")
@RequestMapping("/${ms.manager.path}/cms/friendLink")
public class FriendLinkAction extends BaseAction {
	
	
	@Autowired
	private IFriendLinkBiz linkBiz;

	/**
	 * 返回主界面main
	 */
	 @GetMapping("/main") 
	 public String main(HttpServletResponse response,HttpServletRequest request){ 
		 return "/cms/link/main"; 
	 }
	 
	@RequestMapping("/list")
	@ResponseBody
	public ResultData list(@ModelAttribute @ApiIgnore FriendLinkEntity content, HttpServletResponse response, HttpServletRequest request, @ApiIgnore ModelMap model, BindingResult result) {
		BasicUtil.startPage();
		List contentList = linkBiz.query(content);
		ResultData res = ResultData.build().success(new EUListBean(contentList,(int) BasicUtil.endPage(contentList).getTotal()));
		
		return res;
	}
	
	/**
	 * 返回编辑界面content_form
	 */
	@GetMapping("/form")
	public String form(@ModelAttribute FriendLinkEntity content, HttpServletResponse response, HttpServletRequest request, ModelMap model){
		model.addAttribute("appId", BasicUtil.getApp().getAppId());
		return "/cms/link/form";
	}
	
	@PostMapping("/save")
	@ResponseBody
	@LogAnn(title = "保存友情链接", businessType = BusinessTypeEnum.INSERT)
	@RequiresPermissions("cms:friendlink:save")
	public ResultData save(@RequestBody  FriendLinkEntity link,HttpServletResponse response, HttpServletRequest request) {
		linkBiz.saveLink(link);
		return ResultData.build().success();
	}
	
	@PostMapping("/delete")
	@ResponseBody
	@LogAnn(title = "删除友情链接", businessType = BusinessTypeEnum.DELETE)
	@RequiresPermissions("cms:friendlink:del")
	public ResultData delete(@RequestBody List<String> ids, HttpServletResponse response, HttpServletRequest request) {
		linkBiz.removeByIdss(ids);
		return ResultData.build().success();
	}
	
	
}
